/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kedacom.demo.jmx.helloword;

import com.sun.jdmk.comm.HtmlAdaptorServer;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;

/**
 *
 * @author Administrator
 */
public class HelloAgent implements NotificationListener {

    private MBeanServer mbs = null;

    public HelloAgent() {

        mbs = MBeanServerFactory.createMBeanServer("HelloAgent ");
        mbs = ManagementFactory.getPlatformMBeanServer();//这个有用
        
        ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();

        HtmlAdaptorServer adapter = new HtmlAdaptorServer();

        HelloWorld hw = new HelloWorld();

        ObjectName adapterName = null;

        ObjectName helloWorldName = null;

        try {

            helloWorldName =
                    new ObjectName("HelloAgent :name=helloWorld1");

            mbs.registerMBean(hw, helloWorldName);

            //增加对HelloWorld类的侦听
            hw.addNotificationListener(this, null, null);


            adapterName =
                    new ObjectName("HelloAgent :name=htmladapter,port=9092");

            adapter.setPort(9092);

            mbs.registerMBean(adapter, adapterName);

            adapter.start();

        }
        catch (Exception e) {

            e.printStackTrace();

        }

    }

    //侦听处理函数
    public void handleNotification(
            Notification notif, Object handback) {

        System.out.println("Receiving notification ...");

        System.out.println(notif.getType());

        System.out.println(notif.getMessage());

    }

    public static void main(String args[]) {

        System.out.println("HelloAgent is running");

        HelloAgent agent = new HelloAgent();

    }
}
